package controllers.manage.seat;

import controllers.BaseController;
import controllers.auth.Secure;
import helper.GlobalConfig;
import jodd.util.StringUtil;
import models.constants.DeletedStatus;
import models.merchant.Merchant;
import models.venue.seat.Seat;
import play.modules.paginate.JPAExtPaginator;
import play.mvc.Controller;
import play.mvc.With;

import java.util.HashMap;
import java.util.Map;

/**
 * 座位管理
 * Created by hanbin on 17/4/8.
 */
@With(BaseController.class)
public class SeatCtrl extends Controller {

    /**
     * 跳转到座位管理页面
     */
    public static void index(Integer pageNumber, String seatNo){

        Merchant merchant = Secure.getLoginUser().merchant;

        pageNumber = pageNumber == null ? 1 : pageNumber;
        Map<String , Object> searchMap = new HashMap<>();

        if(StringUtil.isNotBlank(seatNo)) {
            searchMap.put("seatNo", "%"+seatNo+"%");
        }

        if(merchant != null){
            searchMap.put("merchantId", merchant.id);
        }

        String weixin_url = GlobalConfig.WEIXIN_BASE_DOMAIN + "/waterBar/seat";
        flash.put("menuId", "15");
        JPAExtPaginator<Seat> resultPage = Seat.findByCondition(searchMap, "id desc", pageNumber, GlobalConfig.PAGE_SIZE);
        render(resultPage, pageNumber, seatNo , weixin_url);
    }

    /**
     * 跳转到新增座位页面
     */
    public static void add(){
        render();
    }

    /**
     * 跳转到修改座位页面
     */
    public static void modify(long id, Integer pageNumber){
        Seat seat = Seat.findById(id);
        render(seat, pageNumber);
    }


    /**
     * 创建座位
     */
    public static void create(Seat seat){

        Merchant merchant = Secure.getLoginUser().merchant;
        seat.merchant = merchant;

        seat.deleted = DeletedStatus.UN_DELETED;
        seat.save();
        flash.put("msg" , "座位创建成功");
        redirect("/BS0020/admin/seat/index/1");
    }

    /**
     * 修改座位
     */
    public static void update(Long id, Seat seat, Integer pageNumber){
        Seat.update(id , seat);
        flash.put("msg" , "座位修改成功");
        redirect("/BS0020/admin/seat/index/"+pageNumber);
    }

    /**
     * 删除座位
     */
    public static void delete(long id , Integer pageNumber){
        pageNumber = pageNumber == null ? 1 : pageNumber;
        Seat seat = Seat.findById(id);

        seat.deleted=DeletedStatus.DELETED;
        seat.save();
        flash.put("msg" , "座位删除成功");
        redirect("/BS0020/admin/seat/index/"+pageNumber);
    }

    public static void seatOrder(long id , Integer pageNumber){
        Seat seat = new Seat();
        if(id > 0)
            seat = Seat.findBySeatId(id);
        render(seat , pageNumber);
    }



}
